/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.loan;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.loan.CoralAttrib;
import src.services.loan.CollateralModel;
import src.services.loan.CoralAttribModel;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */

@ManagedBean
@SessionScoped
public class CoralAttribBean implements Serializable {

    private List<CoralAttrib> coralAttribs;
    private List<SelectItem> avblCoral;
    private CoralAttrib selectedData, unsaveData;
    private CoralAttribModel attribHelper;
    private boolean editMode=true;

    public CoralAttribBean() {
        this.attribHelper = new CoralAttribModel();
        this.coralAttribs = attribHelper.findAll(null);
        this.avblCoral = new CollateralModel().listAll();
        this.selectedData = null;
        this.unsaveData = null;
    }

    public CoralAttrib getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(CoralAttrib selectedData) {
        this.selectedData = selectedData;
    }

    public CoralAttrib getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(CoralAttrib unsaveData) {
        this.unsaveData = unsaveData;
    }

    public List<SelectItem> getAvblCoral() {
        return avblCoral;
    }

    public List<CoralAttrib> getCoralAttribs() {
        return coralAttribs;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
    }
    
    public void initForm() {
        this.avblCoral = new CollateralModel().listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.unsaveData = CoralAttrib.clone(selectedData);
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.unsaveData = new CoralAttrib();
        this.editMode = false;
    }
    
    public String selectItemValueOf(Integer id) {
        return (String) Utils.selectItemValueOf(avblCoral, id);
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
     
        boolean success = attribHelper.saveData(this.unsaveData);
        if (success==true) {
            if (this.editMode==false) this.coralAttribs.add(unsaveData);
            else {
                int i = coralAttribs.indexOf(selectedData);
                if (i <= coralAttribs.size()) coralAttribs.set(i, CoralAttrib.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setCorattribId(null);
        } 
                        
        Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = attribHelper.deleteData(this.selectedData);
        
        if (success) {
            this.coralAttribs.remove(this.selectedData);
            this.selectedData = null;
        }
               
        Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
}
